package com.i2bt.app.files.model;

import com.i2bt.app.BaseActivity;
import com.i2bt.bean.FilesTransmissionBean;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 
 * Title : 上传文件客户端
 *
 * Description : 连接homebox后按顺序对文件进行上传操作
 *
 * CreatedTime : 2014-9-1
 *
 * @author wuliao & kakaxi & yi
 *
 * @version 1.0
 *
 */
public class FilesUploadClient extends Thread {

	private BaseActivity activity; // 活动类

	private Socket uploadSocket; // 上传客户端

	private FilesTransmissionBean[] transmissionBeans; // 文件路径列表

	private String ip; // ip地址

	private int port; // 端口号

	private String parentPath; // 父路径

	private BufferedInputStream filesLocalInput; // 本地文件读入

	private DataOutputStream filesRemoteOutput; // 远程文件传出

	public FilesUploadClient(BaseActivity activity, String ip, int port, 
			FilesTransmissionBean[] transmissionBeans, String parentPath) {
		// 初始化
		uploadSocket = new Socket();
		this.activity = activity;
		this.ip = ip;
		this.port = port;
		this.transmissionBeans = transmissionBeans;
		this.parentPath = parentPath;
	}

	@Override
	public void run() {
		super.run();

		//连接服务器
		try {
			uploadSocket = new Socket(ip, port);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			activity.toastHandler("找不到homebox");
		} catch (IOException e) {
			e.printStackTrace();
			activity.toastHandler("连接失败");
		}

		int bufferLength = 1024;

		byte[] buffer = new byte[bufferLength];

		int readlength = 0;
		
		// 上传文件名
		String uploadFileName = "";

		// 向服务器获取对应的文件
		for (int i = 0; i < transmissionBeans.length; i++) {
			
			try {
				
				// 进行上传前的输入输出配置
				uploadFileName = transmissionBeans[i].getFileName();
				
				String loadPath = parentPath + "\\" + uploadFileName;

				filesLocalInput = new BufferedInputStream(new FileInputStream(loadPath));

				filesRemoteOutput = new DataOutputStream(
						new BufferedOutputStream(uploadSocket.getOutputStream()));

				int sum = 0;

				activity.toastHandler("开始上传" + uploadFileName);
				
				// 进行文件的上传传输
				while (true) {

					readlength = filesLocalInput.read(buffer, 0, bufferLength);

					if (readlength == -1) {
						break;
					}

					filesRemoteOutput.write(buffer, 0, readlength);

					sum += readlength;

					System.out.println("上传了" + sum);
				}


				filesRemoteOutput.flush();

				activity.toastHandler(uploadFileName + "上传成功!");

			} catch (FileNotFoundException e) {
				e.printStackTrace();
				activity.toastHandler("找不到" + uploadFileName);
			} catch (IOException e) {
				e.printStackTrace();
				activity.toastHandler(uploadFileName + "上传出错!");
			} finally {
				try {
					filesLocalInput.close();
					filesRemoteOutput.close(); 
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
		}
			
		activity.toastHandler("上传完毕!");
		
	}

}
